//
//  KKImageGroupTableViewController.m
//  KKImagePickerController
//
//  Created by Mac mini on 16/1/20.
//  Copyright © 2016年 JuniorCodingPerson. All rights reserved.
//

#import "KKImageGroupsViewController.h"

#import "KKImageLibraryManager.h"

#import "KKImageItemsViewController.h"

static NSString *KKImageGroupTableViewCellIdentifier = @"KKImageGroupTableViewCellIdentifier";

@interface KKImageGroupsViewController () {
    
    NSArray *_kk_assetGroups;
}

@end

@implementation KKImageGroupsViewController

- (void)loadView {
    [super loadView];
    self.tableView.tableFooterView = [UIView new];
    self.tableView.rowHeight = 60.0;
    
    UIRefreshControl *refreshControl = [UIRefreshControl new];
    [refreshControl addTarget: self action: @selector(kk_loadAllGroupsData) forControlEvents: UIControlEventValueChanged];
    self.refreshControl = refreshControl;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self kk_loadAllGroupsData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _kk_assetGroups.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: KKImageGroupTableViewCellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: KKImageGroupTableViewCellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    cell.textLabel.textColor = [UIColor blackColor];
    cell.detailTextLabel.textColor = [UIColor lightGrayColor];
    
    KKImageLibraryGroupItem *groupItem = _kk_assetGroups[indexPath.row];
    
    cell.imageView.image = groupItem.kk_posterImage;
    
    NSString* groupName = [groupItem kk_groupName];
    NSInteger numberOfAssets = [groupItem kk_count];
    
    cell.textLabel.text = [NSString stringWithFormat: @"%@ (%@)", groupName, @(numberOfAssets)];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    KKImageLibraryGroupItem *groupItem = _kk_assetGroups[indexPath.row];
    KKImageItemsViewController *itemsViewController = [KKImageItemsViewController itemsViewController];
    itemsViewController.kk_groupItem = groupItem;
    [self.navigationController pushViewController: itemsViewController animated: true];
}

#pragma mark - Private methods

- (void)kk_loadAllGroupsData {
    
    __weak UITableView *weakTableView = self.tableView;
    __weak UIRefreshControl *weakRefreshControl = self.refreshControl;
    [[KKImageLibraryManager kk_defaultManager] kk_searchAllGroupsFromLibrary:^(NSArray *groups) {
        
        _kk_assetGroups = [NSArray arrayWithArray: groups];
        [weakTableView reloadData];
        [weakRefreshControl endRefreshing];
    }];
}

@end
